/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var allianceSettlementSvc = require("../store/allianceSettlementSvc");
	var Dialog = require("dialog");
	var user = require("store").get("user");
	
    var instance = {
        initComponent: function (params, widget) {
        	var subnav = new Subnav({
        		 parentNode: ".J-subnav",
        		    model: {
        		        title: "入住联盟结算",
        		        items: [{
    						id:"servicePoint",
    						type:"buttongroup",
    						tip:"服务点",
    						keyField :"pkServicePoint",
    						valueField :"name",
//    						lazy:true,
    						items:user.privateServicePoints,
    						params: function(){
    							return{
    								fetchProperties:"pkServicePoint,name"
    							}
    						},
//    						url : "api/servicepoint/operableservicepoints",
    						handler:function(key,element){
    							widget.get("grid").refresh();
    						}
    					},{
    						id : "destination",
    						type:"buttongroup",
    						url:"api/destination/query",
    						all:{
    							show:true,
    							first:true
    						},
    						tip:"目的地",
    						keyField:"pkDestination",
    						params:function(){
    							return{
    								fetchProperties:"pkDestination,name",
    							}
    						},
    						valueField:"name",
    						handler:function(){
    							widget.get("grid").refresh();
    						}
    					},{
    						id : "travelDate",
    						type:"daterange",
    						ranges : {
    					        "本年": [moment().startOf("year"), moment().endOf("days")]
    						},
    						tip:"日期",
    						handler : function(time){
    							 widget.get("grid").refresh();
    						}
    					},{
        		            id: "save",
        		            type: "button",
        		            text: "保存",
        		            show: false,
        		            handler: function() {
        		            	var formData = widget.get("form").getData();
        		            	var gridData = widget.get("editgrid").getData();
        		            	var totalGridData = widget.get("totalGrid").getData();
        		            	allianceSettlementSvc.save(formData,gridData,totalGridData,function(data){
        		            		Dialog.alert({
        		                        content:"保存成功"
        		                    });
        		            		widget.show([".J-grid"]).hide([".J-form",".J-editgrid",".J-total"]);
            		        		widget.get("subnav").hide(["save","return"]).show(["servicePoint","destination","travelDate"]);
            		        		widget.get("form").reset();
            		        		widget.get("grid").refresh();
        		            	});
        		            	
        		            }
        		        }, {
        		            id: "return",
        		            type: "button",
        		            text: "返回",
        		            show: false,
        		            handler: function() {
        		            	widget.show([".J-grid"]).hide([".J-form",".J-editgrid",".J-total"]);
        		        		widget.get("subnav").hide(["save","return"]).show(["servicePoint","destination","travelDate"]);
        		        		widget.get("form").reset();
        		        		widget.get("totalGrid").setDisabled(false);
        		        		widget.get("editgrid").setDisabled(false);
        		            }
        		        }]
        		    }
        	});
        	widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
